package edu.gatech.oad.mule;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

/**
 * This class is the upper most JPanel of the configuration
 * GUI.  It houses the difficulty selection and player
 * selection components.
 * 
 * @author Brendan McGarry, Dale Avery, D'Miria Collins, Natasha Parekh, Jaemin Park
 * @version %I%, %G%
 */
public class UpperConfigPanel extends JPanel{

	//for choosing the difficulty of the game
	private JLabel difficultyLabel;
	private JComboBox difficultySelector;	

	//for choosing the number of players
	private JLabel playersLabel;
	private JComboBox playersSelector;	
	private Integer[] players = 
		new Integer[ConfigurationPanel.MAX_PLAYERS 
		- ConfigurationPanel.MIN_PLAYERS + 1];

	//for choosing the number of players
	private JLabel mapLabel;
	private JComboBox mapSelector;	

	//instance of the big panel
	private ConfigurationPanel cp;

	/**
	 * This constructor creates the upper most panel of
	 * the configuration GUI. It houses the difficulty
	 * and number of player selection components
	 */
	public UpperConfigPanel(ConfigurationPanel cp){
		
		System.out.println("upperConfigPanel");

		//store the instance of the overall panel
		this.cp = cp;

		//give this guy a border so we have a little breathing room
		//  from the other JPanels
		setBorder(new EmptyBorder(
				ConfigurationPanel.PANEL_PADDING,
				ConfigurationPanel.PANEL_PADDING,
				ConfigurationPanel.PANEL_PADDING,
				ConfigurationPanel.PANEL_PADDING
				));

		//create the dropdown for the difficulty and add it to the
		//  panel
		difficultyLabel = new JLabel("Difficulty: ");
		add(difficultyLabel);
		difficultySelector = new JComboBox(ConfigurationPanel.DIFFICULTY);
		difficultySelector.setSelectedIndex(0);
		difficultySelector.setEditable(false);
		add(difficultySelector);

		//create the dropdown for selecting the number of players
		// and add it to the panel
		playersLabel = new JLabel("Players: ");
		add(playersLabel);
		//populates player[] array with how many players can join
		//  the game.  it starts off with the minimum number of
		//  players and goes up the the maximum
		//ex: min = 2, max = 4 => options = {2, 3, 4}
		for(int i = ConfigurationPanel.MIN_PLAYERS; i <= ConfigurationPanel.MAX_PLAYERS; i++){
			players[i - ConfigurationPanel.MIN_PLAYERS] = new Integer(i);
		}//end for	
		playersSelector = new JComboBox(players);
		playersSelector.setSelectedIndex(0);
		playersSelector.setEditable(false);
		//add the listener (see inner class)
		playersSelector.addActionListener(new PlayerSelectionListener());
		add(playersSelector);

		//create the dropdown for the difficulty and add it to the
		//  panel
		mapLabel = new JLabel("Map: ");
		add(mapLabel);
		mapSelector = new JComboBox(ConfigurationPanel.MAP);
		mapSelector.setSelectedIndex(0);
		mapSelector.setEditable(false);
		add(mapSelector);

		System.out.println("/upperConfigPanel");

	}//end constructor

	/**
 	* This class responds to changes in the player selection
	* combo box.
 	* 
 	* @author Brendan McGarry, Dale Avery, D'Miria Collins, Natasha Parekh, Jaemin Park
 	* @version %I%, %G%
 	*/
	private class PlayerSelectionListener implements ActionListener{
		/**
		 * When a new number of players is chosen, we go and 
		 * disable some of the rows so that the user can only
		 * add data for the players that can be edited.
		 */
		public void actionPerformed(ActionEvent e){
			//get the number of players combobox
			JComboBox cb = (JComboBox)e.getSource();
			//figure out how many players are selected
			int playersSelected = ((Integer)cb.getSelectedItem()).intValue();
			System.out.println("Players: [" + playersSelected + "]");
			//go disable the rows that aren't being used
			cp.enableComponents(playersSelected);
		}//end method actionPerformed
	}//end inner class PlayerSelectionListener

	/**
	 * @return The level of difficulty currently selected
	 */ 
	public String getDifficulty(){
		System.out.println("getDifficulty");
		System.out.println("/getDifficulty");
		return (String)(difficultySelector.getSelectedItem());
	}//end method getDifficultyIndex

	/**
	 * @return The type of map selected
	 */
	public String getMap(){
		System.out.println("getMap");
		System.out.println("/getMap");
		return (String)(mapSelector.getSelectedItem());
	}//end method getDIfficulty

	/**
	 * @return The number of players currently selected
	 */
	public int getNumberOfPlayers(){
		System.out.println("getNumberOfPlayers");
		System.out.println("/getNumberOfPlayers");
		return ((Integer)(playersSelector.getSelectedItem())).intValue();
	}//end method getNumberOfPlayers

}//end class UpperConfigPanel
